package com.yu.waterstation.system.modules.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.waterstation.common.common.annotation.AdminLog;
import com.yu.waterstation.common.common.annotation.NeedPermission;
import com.yu.waterstation.common.common.exception.ServiceException;
import com.yu.waterstation.common.common.util.GeneratorUtil;
import com.yu.waterstation.common.common.util.result.Result;
import com.yu.waterstation.system.modules.app.mapper.StudentMapper;
import com.yu.waterstation.common.entity.app.Student;
import com.yu.waterstation.common.entity.system.vo.StudentVO;
import com.yu.waterstation.system.modules.app.service.impl.StudentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * date 2020-12-02 18:16:20
 */
@Api(tags = "系统：学生管理")
@RestController
@RequestMapping("system/student")
public class
StudentController {

    @Resource
    private StudentServiceImpl studentService;
    @Resource
    private StudentMapper studentMapper;

    @ApiOperation("模糊查询")
    @NeedPermission("system:student:list")
    @GetMapping("/search")
    public Result<Page<StudentVO>> searchStudents(
            @RequestParam(required = false) String studentNo,
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize) throws ServiceException {
        return Result.ok(studentService.searchStudents(studentNo, name, pageNo, pageSize));
    }

    @ApiOperation("增加")
    @AdminLog
    @NeedPermission("system:student:add")
    @PostMapping("")
    public Result add(@RequestBody Student student) throws Exception {
        return Result.ok(studentService.addStudent(student));
    }

    @ApiOperation("批量删除")
    @AdminLog
    @NeedPermission("system:student:delete")
    @DeleteMapping("/batch")
    public Result delete(@RequestBody List<Integer> idList) throws ServiceException {
        return Result.ok(studentService.deleteStudentBatchIds(idList));
    }

    @ApiOperation("修改")
    @AdminLog
    @NeedPermission("system:student:update")
    @PutMapping("")
    public Result update(@RequestBody Student student) throws ServiceException {
        return Result.ok(studentService.updateStudent(student));
    }

    @ApiOperation("冻结|激活账号")
    @AdminLog
    @NeedPermission("system:student:update")
    @PutMapping("/status/{studentId}")
    public Result updateStudentStatus(@PathVariable Integer studentId) throws ServiceException {
        return Result.ok(studentService.updateStudentStatus(studentId));
    }

    @ApiOperation("寝室成员")
    @NeedPermission("system:address:member:list")
    @GetMapping("/address/member/{addressId}")
    public Result getMembersOfAddress(@PathVariable Integer addressId) {
        return Result.ok(studentService.getMembersOfAddress(addressId));
    }

    @ApiOperation("重置学生寝室(或移除寝室)")
    @AdminLog
    @NeedPermission("system:student:update")
    @DeleteMapping("/address/{studentId}")
    public Result<Integer> removeAddressOfStudent(@PathVariable Integer studentId) {
        return Result.ok(studentMapper.removeAddressOfStudent(studentId));
    }


    @ApiOperation("重置密码")
    @AdminLog
    @NeedPermission("system:student:update")
    @PutMapping("/password/reset/{studentId}")
    public Result<Integer> resetStudentPassword(@PathVariable Integer studentId)   {
        return Result.ok(studentService.resetStudentPassword(studentId));
    }

}
